<script setup>
import { ref, reactive, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { Plus } from '@element-plus/icons-vue'
import * as admin from '@/api/admin.js'
import { useTokenStore } from '@/stores/token.js';
const tokenStore = useTokenStore();

// 模拟数据
const reviews = ref([]);
const currentPage = ref(1);
const pageSize = ref(10);
const total = ref(0);
const search_comment = ref('')
const search_rating = ref('')

const fetchReviews = async() => {
  const param = {
      currentPage: currentPage.value,
      comment: search_comment.value,
      rating: search_rating.value,
  }
  const res = await admin.findReview(param)
  reviews.value = res.data;
  total.value = res.total;
};

const searchReviews = () => {
  fetchReviews();
};

const deleteReview = (id) => {
  const param = {
    id: id
  }
  ElMessageBox.confirm('确认删除该评价？', '提示', { type: 'warning' })
    .then(async () => {
      const res = await admin.delReview(param)
      if (res.code == 0) {
        fetchReviews();
      }
      ElMessage.success('删除成功');
    })
    .catch(() => ElMessage.info('取消删除'));
};

const handlePageChange = (page) => {
  currentPage.value = page;
  fetchReviews();
};

onMounted(() => {
  fetchReviews();
});
</script>

<template>
  <div class="page">
    <el-card class="search-box">
      <el-form :inline="true" class="search-form">
        <el-form-item label="评价信息">
          <el-input v-model="search_comment" placeholder="请输入评价信息" clearable />
        </el-form-item>
        <el-form-item label="评分">
          <el-input v-model="search_rating" placeholder="请输入评分" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="searchReviews" icon="el-icon-search">搜索</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-table :data="reviews" border>
      <el-table-column prop="product" label="产品" />
      <el-table-column prop="yonghu" label="用户" />
      <el-table-column prop="rating" label="评分" width="120" />
      <el-table-column prop="comment" label="评论内容" />
      <el-table-column prop="createdAt" label="评价时间" width="150" />
      <el-table-column label="操作" width="180">
        <template #default="{ row }">
          <!-- <el-button type="primary" size="small" @click="editReview(row.id)">修改</el-button> -->
          <el-button type="danger" size="small" @click="deleteReview(row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      :total="total"
      :page-size="pageSize"
      @current-change="handlePageChange"
    />
  </div>
</template>

<style scoped>
.page {
  padding: 30px;
  background: #f7f7f7;
}

.search-box {
  background: #fff;
  border-radius: 10px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  padding: 20px;
  margin-bottom: 20px;
}

.add-button {
  background-color: #67c23a;
  color: white;
  border-radius: 8px;
  font-size: 14px;
}

.el-button {
  margin: 0 5px;
}

.el-table {
  margin-top: 20px;
}

.el-pagination {
  margin-top: 20px;
  text-align: center;
}
</style>
